package com.uzhie.web.service.listener;

import com.uzhie.component.GpsComponent;
import com.uzhie.component.WarningComponent;
import com.uzhie.jt808.msg.entity.JT_0200;
import com.uzhie.jt808.msg.entity.JT_200.JS_0200;
import com.uzhie.web.module.base.Tree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.Message;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by zhenghao on 2017/7/4.
 */
@Component(value = "consumerMessageListener")
public class MQPositionReceiveService implements MQReceiveService {

    private Timer positionTimer = new Timer("MQReceiveServicePositionTimer", true);
    private long checkTime;
    private long overTime;

    @Autowired
    GpsComponent gpsComponent;
    @Autowired
    WarningComponent warningComponent;

    public void setCheckTime(long checkTime) {
        this.checkTime = checkTime;
    }

    public void setOverTime(long overTime) {
        this.overTime = overTime;
    }

    @Override
    public void onMessage(Message message) {
        LOGGER.fatal("收到位置信息消息");
        handlerJT_0200(message);
    }


    //处理位置信息
    private void handlerJT_0200(Message msg) {
        try {
            BytesMessage bytesMessage = (BytesMessage) msg;
            if (bytesMessage.getBodyLength() > 0) {
                LOGGER.info(" 收到了位置信息--->");
                byte[] body = new byte[(int) bytesMessage.getBodyLength()];
                LOGGER.info(" 位置信息消息长度 : [" + bytesMessage.getBodyLength() + "]--->");
                bytesMessage.readBytes(body);
                String mSimNumber = msg.getStringProperty(PARAM);
                LOGGER.info(" 位置信息的@车辆内置SIM卡@ 卡号为 : [" + mSimNumber + "] --->");
                JT_0200 order = new JS_0200();
                order.ReadFromBytes(body);
                LOGGER.info(" 转换后的 位置信息内容为 : [ " + order.toString() + " ] --->");
                handlerPosition(order, mSimNumber);
                handlerWarns(order, mSimNumber);
            }
        } catch (JMSException e) {
            LOGGER.error("位置信息消息转义错误", e);
        }
    }

    private void handlerWarns(JT_0200 order, String simNumber) {
        if (order.isMediaWarning() != 0) {

            warningComponent.addWarning(order, simNumber);
        }
        if (order.isJSWarning()) {
            LOGGER.info("有苏标报警 正在处理");
            JS_0200 js0200 = (JS_0200) order;
            warningComponent.addJSWarning(js0200, simNumber);
        }
        warningComponent.addWarningCollection(simNumber);
    }

    private void handlerPosition(JT_0200 order, String simNumber) {
        gpsComponent.addGpsRecord(simNumber, order);
    }

    /**
     * 定位信息存储task
     */
    protected void positionTask() {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                try {
                    gpsComponent.gpsInsertTask();
                } catch (Exception e) {
                    LOGGER.error("位置信息批量存储异常", e);
                }
            }
        };
        positionTimer.schedule(task, checkTime, checkTime);
        LOGGER.warn("位置信息服务启用成功");
    }


    /**
     * 定位存储任务销毁
     */
    protected void onDestroy() {
        if (positionTimer != null) {
            positionTimer.cancel();
            positionTimer = null;
            LOGGER.warn("位置信息服务停止成功");
        }
    }

}
